
#include <sys/timeb.h>
#include "Globle.h"
#include "GraphCutSegmentLazy.h"
#include "function.h"
#include "mLinkList.h"


IplImage* inImage     = NULL;
IplImage* markupImage = NULL;
IplImage* outImage    = NULL;

int wIn = 0, hIn = 0;
unsigned char fgcolor[] = { 255, 0, 0 };
unsigned char bgcolor[] = { 0, 0, 255 };

timeb t1,t2;
bool drawing_point = false;
bool changed = false;
CvScalar color;
CvPoint point;
void my_mouse_callback(int event, int x, int y, int flags, void* param );
set<sPoint> *objPt = new set<sPoint>;
set<sPoint> *bacPt = new set<sPoint>;

IplImage *result;
int main(int argc, char* argv[])
{
	
	float scale = 1.0;
	objPt->clear();
	bacPt->clear();
	IplImage *src = cvLoadImage("e:\\data\\Lena.jpg");

	inImage = cvCreateImage(cvSize(src->width/scale,src->height/scale),src->depth,src->nChannels);
	

	cvResize(src,inImage);
	if(!inImage ) {
		cout << "Error loading the image" << endl;
		return 0;
	}
	IplImage *image = cvCloneImage(inImage);
	outImage    = cvCreateImage(cvGetSize(inImage),inImage->depth,inImage->nChannels);
	markupImage = cvCreateImage(cvGetSize(inImage),inImage->depth,inImage->nChannels);
	cvZero(markupImage);
	color = CV_RGB(0,0,255);
	cvLine(image,cvPoint(0,0),cvPoint(0,0),color,2,CV_AA,0);
	cvZero(outImage);

	cvNamedWindow("LazySnapping");
	cvSetMouseCallback("LazySnapping",my_mouse_callback,(void*) image);
	
	while(1)
	{		
		cvShowImage("LazySnapping",image);
		char c = cvWaitKey(20);
		if(  c == 27)
			break;
		if (changed)
		{			
			changed = false;
		}
		if ( c == 'p' || c == 'P')
		{		
			cout<<"object:\n";
			PrintfSetSPoint(objPt);
			cout<<"background:\n";
			PrintfSetSPoint(bacPt);
		}
		if ( c == '\r')
		{
			objPt->clear();
			bacPt->clear();
			GetSeedsPoint(markupImage,objPt,bacPt);
			GraphCutSegmentLazy Lazy(inImage,objPt,bacPt);	
			Lazy.graphcut();			
		}
		if ( c == 'R' || c == 'r')
		{
			objPt->clear();
			bacPt->clear();
			cvCopy(inImage,image);
			cvZero(markupImage);
		}
	}
	return 0;
}

void my_mouse_callback(int event, int x, int y, int flags, void* param )
{
	IplImage* image = (IplImage*) param;

	if (x < 0 || y < 0 || x >= image->width || y > image->height)
	{		
		drawing_point = false;
		return;
	}

	switch( event )
	{
	case CV_EVENT_MOUSEMOVE:
		{
			if( drawing_point )
			{
				cvLine(image,point,cvPoint(x,y),color,4,CV_AA,0);
				cvLine(markupImage,point,cvPoint(x,y),color,4,CV_AA,0);
				point = cvPoint(x,y);
			}
		}
		break;
	case CV_EVENT_LBUTTONDOWN:
		{
			changed = true;
			drawing_point = true;
			point = cvPoint(x,y);
			color = CV_RGB(255,0,0);
		}
		break;
	case CV_EVENT_LBUTTONUP:
		{
			if (drawing_point)
			{
				drawing_point= false;
				cvLine(image,point,cvPoint(x,y),color,4,CV_AA,0);
				cvLine(markupImage,point,cvPoint(x,y),color,4,CV_AA,0);
			}
		}
		break;
	case CV_EVENT_RBUTTONDOWN:
		{
			changed       = true;
			point         = cvPoint(x,y);
			drawing_point = true;
			color         = CV_RGB(0,0,255);
		}
		break;
	case CV_EVENT_RBUTTONUP:
		{
			if (drawing_point)
			{
				drawing_point = false;
				cvLine(image,point,cvPoint(x,y),color,4,CV_AA,0);
				cvLine(markupImage,point,cvPoint(x,y),color,4,CV_AA,0);
				cvSaveImage("markup2.bmp",markupImage,0);
			}
			break;			
		}
	}
}

